package com.longtop.perform.ar.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.longtop.framework.base.BaseService;
import com.longtop.framework.common.Constant;
import com.longtop.framework.security.BaseCheckedException;
import com.longtop.framework.security.SessionObject;
import com.longtop.framework.util.ReflectUtil;
import com.longtop.framework.util.SpringBeanFactory;

public class AssignMxService extends BaseService {

	public static final String BEAN_ID = "assignMxService";

	public List queryDetail(String hql) {
		List list = queryAll(hql);
		return list;
	}

	/**
	 * 通用保存分配明细
	 * 
	 * @param changeMap
	 * @param sessionObject
	 *            用户对象
	 * @param fkId
	 *            分配主表ID
	 * @param masterClass
	 *            分配主表类名
	 * @throws BaseCheckedException
	 * @throws ServiceException
	 * @author SUN
	 */
	public void saveDetail(Map changeMap, SessionObject sessionObject, String fkId, Class masterClass) throws BaseCheckedException {

		// 删除主键列表、更新bean的列表、新增bean的列表
		List insertBeanList = (ArrayList) changeMap.get(Constant.INSERT_BEAN_LIST);
		List updateBeanList = (ArrayList) changeMap.get(Constant.UPDATE_BEAN_LIST);
		List deleteBeanList = (ArrayList) changeMap.get(Constant.DELETE_BEAN_LIST);
		Object entity;
		Iterator iterator;

		// 批量插入记录
		if (insertBeanList.size() > 0) {
			iterator = insertBeanList.iterator();
			while (iterator.hasNext()) {
				entity = iterator.next();
				if (!"".equals(fkId)) {// 说明主表有新增，回算验证已经在主表新增时验证过了
					ReflectUtil.setFieldValue(entity, "fkId", String.class, fkId);
				} else {// 只新增子表时,需验证回算
					fkId = "" + ReflectUtil.getFieldValue(entity, "fkId");
					Object masterEntity = this.get(masterClass, fkId);
					AssignMasterService masterService = SpringBeanFactory.getBean(AssignMasterService.BEAN_ID);
					if (masterService.recalcDispose(masterEntity, true)) {
						throw new BaseCheckedException("新增失败，业绩分配开始时间超过回算时间，不允许此操作。");
					}
				}
				entity = super.setAuthPropetryToEntity(entity, sessionObject, true);
				super.save(entity);
			}
		}

		// 批量更新记录
		if (updateBeanList.size() > 0) {
			iterator = updateBeanList.iterator();
			while (iterator.hasNext()) {
				entity = iterator.next();
				// 需验证回算
				fkId = "" + ReflectUtil.getFieldValue(entity, "fkId");
				Object masterEntity = this.get(masterClass, fkId);
				AssignMasterService masterService = SpringBeanFactory.getBean(AssignMasterService.BEAN_ID);
				if (masterService.recalcDispose(masterEntity, true)) {
					throw new BaseCheckedException("更新失败，业绩分配开始时间超过回算时间，不允许此操作。");
				}
				entity = super.setAuthPropetryToEntity(entity, sessionObject, false);

				super.update(entity);
			}
		}
		// 批量删除记录
		if (deleteBeanList.size() > 0) {
			iterator = deleteBeanList.iterator();
			while (iterator.hasNext()) {
				entity = iterator.next();
				// 需验证回算
				fkId = "" + ReflectUtil.getFieldValue(entity, "fkId");
				Object masterEntity = this.get(masterClass, fkId);
				AssignMasterService masterService = SpringBeanFactory.getBean(AssignMasterService.BEAN_ID);
				if (masterService.recalcDispose(masterEntity, true)) {
					throw new BaseCheckedException("删除失败，业绩分配开始时间超过回算时间，不允许此操作。");
				}
				super.delete(entity);

			}
		}
	}

	/**
	 * 通用保存分配明细(流水分配)
	 * 
	 * @param changeMap
	 * @param sessionObject
	 *            用户对象
	 * @param fkId
	 *            分配主表ID
	 * @param beanclass
	 *            分配子表类名
	 * @throws BaseCheckedException
	 * @author SUN
	 */
	public void saveMasterDetail(Map changeMap, SessionObject sessionObject, String fkId, Class masterClass) throws BaseCheckedException, Exception {
		// 插入成功的记录数、更新成功的记录数、删除成功的记录数
		int insertCount = 0;
		int updateCount = 0;
		int deleteCount = 0;

		// 删除主键列表、更新bean的列表、新增bean的列表
		List insertBeanList = (ArrayList) changeMap.get(Constant.INSERT_BEAN_LIST);
		List updateBeanList = (ArrayList) changeMap.get(Constant.UPDATE_BEAN_LIST);
		String idList = (String) changeMap.get(Constant.ID_LIST);
		Object detailBean;
		Iterator iterator;
		Object masterEntity = masterClass.newInstance();
		// 是否有主表ID

		// 批量插入记录
		if (insertBeanList.size() > 0) {
			// 流水回算处理
			iterator = insertBeanList.iterator();
			while (iterator.hasNext()) {
				detailBean = iterator.next();
				String resId = "" + ReflectUtil.getFieldValue(detailBean, "resId");
				String resOrg = "" + ReflectUtil.getFieldValue(detailBean, "resOrg");
				Date startDate = (Date) ReflectUtil.getFieldValue(detailBean, "startDate");
				Date endDate = (Date) ReflectUtil.getFieldValue(detailBean, "endDate");
				if (fkId == null || "".equals(fkId)) {
					// 保存主表
					ReflectUtil.setFieldValue(masterEntity, "resId", String.class, resId);
					ReflectUtil.setFieldValue(masterEntity, "resOrg", String.class, resOrg);
					ReflectUtil.setFieldValue(masterEntity, "startDate", Date.class, startDate);
					ReflectUtil.setFieldValue(masterEntity, "endDate", Date.class, endDate);
					AssignMasterService masterService = SpringBeanFactory.getBean(AssignMasterService.BEAN_ID);
					if (masterService.recalcDispose(masterEntity, true)) {
						throw new BaseCheckedException("新增失败，流水交易日期超过回算时间，不允许此操作。");
					}
					super.save(masterEntity);
					fkId = "" + ReflectUtil.getFieldValue(masterEntity, "id");
				} else {
					// 获取主表信息
					masterEntity = this.get(masterClass, fkId);
					AssignMasterService masterService = SpringBeanFactory.getBean(AssignMasterService.BEAN_ID);
					if (masterService.recalcDispose(masterEntity, true)) {
						throw new BaseCheckedException("新增失败，流水交易日期超过回算时间，不允许此操作。");
					}
				}
				ReflectUtil.setFieldValue(detailBean, "fkId", String.class, fkId);
				detailBean = super.setAuthPropetryToEntity(detailBean, sessionObject, true);
				super.save(detailBean);
				insertCount++;
			}
		}
		// 批量更新记录
		if (updateBeanList.size() > 0) {
			// 需验证回算
			masterEntity = this.get(masterClass, fkId);
			AssignMasterService masterService = SpringBeanFactory.getBean(AssignMasterService.BEAN_ID);
			if (masterService.recalcDispose(masterEntity, true)) {
				throw new BaseCheckedException("更新失败，流水交易日期超过回算时间，不允许此操作。");
			}

			iterator = updateBeanList.iterator();
			while (iterator.hasNext()) {
				detailBean = iterator.next();
				detailBean = super.setAuthPropetryToEntity(detailBean, sessionObject, false);
				super.update(detailBean);
				updateCount++;
			}
		}

		// 批量删除记录
		if (idList.length() > 0) {
			// 需验证回算
			masterEntity = this.get(masterClass, fkId);
			AssignMasterService masterService = SpringBeanFactory.getBean(AssignMasterService.BEAN_ID);
			if (masterService.recalcDispose(masterEntity, true)) {
				throw new BaseCheckedException("删除失败，流水交易日期超过回算时间，不允许此操作。");
			}
			String sql = "delete from " + masterClass.getSimpleName().replaceAll("Master", "Detail") + " where id in(" + idList + ")";
			this.batchUpdate(sql);

			// 验证若没子表记录,则删除主表记录
			String hql = "from " + masterClass.getSimpleName().replaceAll("Master", "Detail") + " where fkId ='" + fkId + "'";
			List list = this.queryAll(hql);
			if (list.size() == 0) {
				super.delete(masterEntity);
			}
		}
	}
}
